package com.xie.controller;

import com.xie.pojo.Student;
import com.xie.service.StudentService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;

    @RequestMapping("/queryAllStudent")
    public String queryAllStudent(HttpSession session){
        List<Student> students = studentService.queryAllStudent();
        session.setAttribute("studentList",students);
        return "student";
    }

    @RequestMapping("/toAddStudent")
    public String toAddStudent(){
        return "addStudent";
    }
    @RequestMapping("addStudent")
    public String addTask(Student student, Model model){
        System.out.println("controller  student :" + student);
        int i = studentService.addStudent(student);
        if (i > 0){
            return "redirect:/student/queryAllStudent";
        }else{
            model.addAttribute("msg","添加失败");
            return "addStudent";
        }
    }

    @RequestMapping("/deleteStudent")
    public String deleteStudent(String sid, Model model){
        int i = studentService.deleteStudent(sid);
        if(i > 0){
            return "redirect:/student/queryAllStudent";
        }else{
            model.addAttribute("msg","删除失败");
            return "redirect:/student/queryAllStudent";
        }
    }

    @RequestMapping("/toUpdateStudent")
    public String toUpdateStudent(String sid,Model model){
        Student student = studentService.queryStudent(sid);
        model.addAttribute("QStudent",student);
        return "updateStudent";
    }
    @RequestMapping("/updateStudent")
    public String updateStudent(Student student,Model model){
        int i = studentService.updateStudent(student);
        if(i > 0){
            return "redirect:/student/queryAllStudent";
        }else{
            model.addAttribute("msg","修改失败");
            return "redirect:/student/queryAllStudent";
        }
    }

    @RequestMapping("/toExcelAdd")
    public String toExcelAdd(){
        return "upload";
    }
    @RequestMapping("/excelAdd")
    public String excelAdd(@RequestParam("subExcelFile") CommonsMultipartFile file) throws IOException {
        if (file != null){
            InputStream inputStream = file.getInputStream();//获取文件

            Workbook workbook = new XSSFWorkbook(inputStream);//创建工作簿
            Sheet sheet = workbook.getSheetAt(0);

//            Row row = sheet.getRow(1);
//            Cell cell = row.getCell(0);
//            String value = cell.getStringCellValue();
//            System.out.println(value);
            int rowCount = sheet.getPhysicalNumberOfRows();

            for (int rowNum = 1; rowNum < rowCount; rowNum++) {
                Student student = new Student();
                Row row = sheet.getRow(rowNum);
                if (row != null){
                    int cellCount = row.getPhysicalNumberOfCells();
                    for (int cellNum = 0; cellNum < cellCount; cellNum++) {
                        Cell cell = row.getCell(cellNum);
                        String cellValue = "";
                        if (cell != null){
                            int cellType = cell.getCellType();
                            switch (cellType){
                                case XSSFCell.CELL_TYPE_STRING://字符串
                                    System.out.print("[STRING]");
                                    cellValue = cell.getStringCellValue();
                                    break;
                                case XSSFCell.CELL_TYPE_BOOLEAN://布尔
                                    System.out.print("[BOOLEAN]");
                                    cellValue = String.valueOf(cell.getBooleanCellValue());
                                    break;
                                case XSSFCell.CELL_TYPE_BLANK://空
                                    System.out.print("[BLANK]");
                                    break;
                                case XSSFCell.CELL_TYPE_NUMERIC://数字（日期、普通数字）
                                    System.out.print("[NUMERIC]");
                                    if (HSSFDateUtil.isCellDateFormatted(cell)){
                                        System.out.print("[日期]");
                                        Date date = cell.getDateCellValue();
                                        cellValue = new DateTime(date).toString("yyyy-MM-dd HH:mm:ss");
                                    }else{
                                        System.out.print("[转换成字符串输出]");
                                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                                        cellValue = cell.toString();
                                    }
                                    break;
                                case XSSFCell.CELL_TYPE_ERROR:
                                    System.out.print("[数据类型错误]");
                                    break;
                            }
                            System.out.println(cellValue);
                            switch (cellNum){
                                case 0:
                                    student.setSid(cellValue);
                                    break;
                                case 1:
                                    student.setSname(cellValue);
                                    break;
                                case 2:
                                    student.setPwd(cellValue);
                                    break;
                                case 3:
                                    student.setClassname(cellValue);
                                    break;
                                case 4:
                                    student.setSex(cellValue);
                                    break;
                                case 5:
                                    student.setAge(cellValue);
                                    break;
                                case 6:
                                    student.setPhone(cellValue);
                                    break;
                            }
                        }

                    }

                }
//                System.out.println("----" + student);
                studentService.addStudent(student);
            }

            inputStream.close();
        }
        return "redirect:/student/queryAllStudent";
    }














}
